package com.frogs.ucenter.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.PropertyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.frogs.framework.base.web.BaseController3;
import com.frogs.framework.util.DWZResponse;
import com.frogs.framework.util.PageList;
import com.frogs.framework.util.RenderUtil;
import com.frogs.framework.web.HttpRequestInfo;
import com.frogs.ucenter.entity.IntegralPromotionGoodsRelation;
import com.frogs.ucenter.service.InsuranceInfoService;
import com.frogs.ucenter.service.IntegralPromotionGoodsRelationService;

@Controller
@RequestMapping("/admin/integralPromotionGoodsRelation")
public class IntegralPromotionGoodsRelationController extends BaseController3 {
    private static final String ROOT_VIEW = "admin/integralPromotionGoodsRelation";

    private static final String PROMOTION_LIST = "/promotionList";
    
    private static final String PROMOTION_LIST_VIEW = ROOT_VIEW +"/listCoupon";
    
    private static final String HOME_VIEW = ROOT_VIEW + "/home";

    private static final String EDIT_VIEW = ROOT_VIEW + "/edit";
    
    private static final String INSURANCE_LIST = "/insuranceList";
    
    private static final String INSURANCE_LIST_VIEW = ROOT_VIEW + "/listInsurance";

    @Autowired
    private IntegralPromotionGoodsRelationService integralPromotionGoodsRelationService;
    @Resource
    private InsuranceInfoService insuranceInfoService;

    @RequestMapping()
    public String listIntegralPromotionGoodsRelation(HttpServletRequest request, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        setRequestModelMap( request, model, true );
        getPageList( model );
        return HOME_VIEW;
    }

    @RequestMapping(EDIT)
    public String createIntegralPromotionGoodsRelation(HttpServletRequest request, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        setRequestModelMap( request, model, false );
        Integer id = reqInfo.getIntParameter( "id", -1 );
        if( id > 0 ){
            model.put( "model",integralPromotionGoodsRelationService.getById( id ) ); 
        }
        return EDIT_VIEW;
    }

    @RequestMapping(ADD)
    public String saveIntegralPromotionGoodsRelation(HttpServletRequest request, HttpServletResponse response, ModelMap model, IntegralPromotionGoodsRelation integralPromotionGoodsRelation) {
        setRequestModelMap(request, model, false);
        DWZResponse.Builder builder;
        boolean flag = true;
        try {
            integralPromotionGoodsRelationService.insert( integralPromotionGoodsRelation ); 
            builder = DWZResponse.getSucessBuilder("新增成功");
        } catch (Exception e) {
        	flag = false;
            builder = DWZResponse.getFailBuilder("新增失败" + e.getMessage());
        }
        RenderUtil.renderHtml(builder.build().toString(), response);
        request.setAttribute("flag", flag);
        return null;
    }

    @RequestMapping(SHOW)
    public String showIntegralPromotionGoodsRelation(HttpServletRequest request, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        Integer id = reqInfo.getIntParameter( "id", -1 );
        if( id > 0 ){
            model.addAttribute( integralPromotionGoodsRelationService.getById( id ) ); 
        }
        setRequestModelMap(request, model);
        return null;
    }

    @RequestMapping(UPDATE)
    public String updateIntegralPromotionGoodsRelation(HttpServletRequest request, HttpServletResponse response, ModelMap model, IntegralPromotionGoodsRelation integralPromotionGoodsRelationNew) throws Exception {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        Integer id = reqInfo.getIntParameter( "id", -1 );
        boolean flag = true;
        if( id > 0 ){
            DWZResponse.Builder builder;
            try {
                IntegralPromotionGoodsRelation integralPromotionGoodsRelation = integralPromotionGoodsRelationService.getById( id ); 
                PropertyUtils.copyProperties(integralPromotionGoodsRelation, integralPromotionGoodsRelationNew);
                integralPromotionGoodsRelationService.update( integralPromotionGoodsRelation ); 
                model.clear();
                builder = DWZResponse.getSucessBuilder("更新成功");
            } catch (Exception e) {
            	flag = false;
                builder = DWZResponse.getFailBuilder("更新失败" + e.getMessage());
            }
            RenderUtil.renderHtml(builder.build().toString(), response);
        }
        request.setAttribute("flag", flag);
        return null;
    }

    @RequestMapping(DELETE)
    public String deleteIntegralPromotionGoodsRelation(HttpServletRequest request, HttpServletResponse response, ModelMap model, @RequestParam(required=true, value="ids")String inIdList) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        setRequestModelMap(request, model);
        DWZResponse.Builder builder;
        boolean flag = true;
        try {
            String[] idArray = inIdList.split(",");
            model.put("inIdList", idArray);
            integralPromotionGoodsRelationService.delete( model ); 
            builder = DWZResponse.getSucessBuilder("删除成功");
        } catch (Exception e) {
        	flag = false;
            builder = DWZResponse.getFailBuilder("删除失败" + e.getMessage());
        }
        RenderUtil.renderHtml(builder.build().toString(), response);
        model.clear();
        request.setAttribute("flag", flag);
        return null;
    }
    
    @RequestMapping(PROMOTION_LIST)
    public String getPromotionList(HttpServletRequest request, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        int sendCardType = reqInfo.getIntParameter("sendCardType", 0);//卡券发放类型
        int showType = reqInfo.getIntParameter("showType", 0);
        int pageNum = reqInfo.getIntParameter("pageNum", 0);
        int numPerPage = reqInfo.getIntParameter("numPerPage", 1000);
        model.put("promotionList", integralPromotionGoodsRelationService.getPromotionList(sendCardType, pageNum, numPerPage));
        setRequestModelMap(request, model);
        return PROMOTION_LIST_VIEW;
    }
    
    @RequestMapping(INSURANCE_LIST)
    public String getInsuranceList(HttpServletRequest request, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        int pageNo = reqInfo.getIntParameter("pageNum", 1);
        int pageSize = reqInfo.getIntParameter("numPerPage", 10);
        Map<String, Object> params = new HashMap<String, Object>();
        model.put("insuranceList", insuranceInfoService.getList(params, pageNo, pageSize));
        setRequestModelMap(request, model);
        return INSURANCE_LIST_VIEW;
    }

    private PageList<IntegralPromotionGoodsRelation> getPageList(ModelMap model) {
        PageList<IntegralPromotionGoodsRelation> integralPromotionGoodsRelationList = integralPromotionGoodsRelationService.getList( model, Integer.parseInt(model.get("pageNum").toString()), Integer.parseInt(model.get("numPerPage").toString()) ); 
        model.put("integralPromotionGoodsRelationList",integralPromotionGoodsRelationList);
        model.put("pageTurn",integralPromotionGoodsRelationList.getPageTurn());
        return integralPromotionGoodsRelationList;
    }
    
}